#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "export.h"
#include "vectors.h"

#if !defined (_fileio_util_h)
#define _fileio_util_h

/* Some types */

typedef char                    int8;
typedef unsigned char           uint8;
typedef short int               int16;
typedef unsigned short int      uint16;
typedef int                     int32;
typedef unsigned int            uint32;
typedef float                   float32;
typedef double                  float64;
#ifdef _WIN32
typedef __int64                 int64;
typedef unsigned __int64        uint64;
#else
typedef long long               int64;
typedef unsigned long long      uint64;
#endif


typedef struct {

  int32 ntags;

  char *structName;
  char **tagNames;

  /* Offset of each tag in memory */
  IDL_MEMINT *tagOffsets;

  /* Tag descriptor */
  IDL_VPTR *tagDesc;

  /* Length of a row in the file. This is sum over tagNbytes. Not same 
     as number of bytes in actual structure def, which has overhead */
  int32  row_nbytes;

  /* Number of bytes in tag.  Note, this does not reflect overhead such
     as for string definition structures */
  int32 *tagNbytes;

  /* Number of elements in tag */
  int32 *tagNelts;

  /* buffer for copying string data */
  char **buffer;

  /* Structure definition */
  IDL_StructDefPtr sdef;

} idl_tag_info;


/* typedefs */


void error_message(char *text, char *errMessage);
void error_message_where(int row, int column);
void message(char *text);

FILE *get_fileptr(IDL_VPTR fileVptr, char *mode);

int rowisgood(IDL_MEMINT row, IDL_MEMINT nrows);


IDL_MEMINT_VECTOR *getrows(IDL_VPTR rowsInputVptr,
			   IDL_MEMINT nrows);

IDL_MEMINT *getcolumns(IDL_VPTR columnsInputVptr,
		       IDL_VPTR *columnsVptr, 
		       IDL_MEMINT *ncolumns,
		       int8 *columns_converted);
UCHAR *get_output(IDL_VPTR instructVptr,
		  int columns_there, IDL_VPTR columnsInputVptr,
		  idl_tag_info *input_tag_info,
		  IDL_MEMINT nrows2read,
		  IDL_VPTR *resultVptr,
		  IDL_MEMINT **tagOffsets,
		  int8 **get_column, int verbose);

idl_tag_info *get_idl_tag_info(IDL_VPTR structSrc, int verbose);
void print_idl_type(int type);
int32
idl_type_nbytes(int type);
IDL_STRUCT_TAG_DEF *
getColumnsTagDef(IDL_VPTR structSrc, idl_tag_info *tag_info, 
		 IDL_MEMINT *columns, IDL_MEMINT ncolumns, 
		 int8 *get_column, int *nkeep);
void
free_tag_info(idl_tag_info *tag_info);

#endif /* _fileio_util_h */
